package com.epfly.base.util;


import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;

public class DateUtil {

    public static final String COMPACT_TIME_FORMAT = "yyyyMMddHHmmss";
    public static final String COMPACT_DATE_FORMAT = "yyyyMMdd";

    /**
     * 取当前日期 yyyyMMdd
     */
    public static int getCurrentDate() {
        LocalDate localDate = LocalDate.now(ZoneId.systemDefault());
        return Integer.parseInt(localDate.format(DateTimeFormatter.ofPattern(COMPACT_DATE_FORMAT)));
    }

    /**
     * 取当前时间 yyyyMMddHHmmss
     */
    public static long getCurrentTime() {
        LocalDateTime localDateTime = LocalDateTime.now(ZoneId.systemDefault());
        return Long.parseLong(localDateTime.format(DateTimeFormatter.ofPattern(COMPACT_TIME_FORMAT)));
    }

    public static long getCurrentTimeWithPlusMinutes(long increment) {
        LocalDateTime now = LocalDateTime.now(ZoneId.systemDefault());
        LocalDateTime incrementDateTime = now.plusMinutes(increment);
        String ts = incrementDateTime.format(DateTimeFormatter.ofPattern(COMPACT_TIME_FORMAT));
        return Long.parseLong(ts);
    }

    public static long getCurrentTimeWithMinusMinutes(long decrement) {
        LocalDateTime now = LocalDateTime.now(ZoneId.systemDefault());
        LocalDateTime incrementDateTime = now.minusMinutes(decrement);
        String ts = incrementDateTime.format(DateTimeFormatter.ofPattern(COMPACT_TIME_FORMAT));
        return Long.parseLong(ts);
    }

    public static int getCurrentDateWithPlusDays(long increment) {
        LocalDate now = LocalDate.now(ZoneId.systemDefault());
        LocalDate incrementDateTime = now.plusDays(increment);
        String ts = incrementDateTime.format(DateTimeFormatter.ofPattern(COMPACT_DATE_FORMAT));
        return Integer.parseInt(ts);
    }


    /**
     * 根据提供的时间(yyyyMMddHHmmss)取日期(yyyyMMdd)
     */
    public static Integer getDateByProvidedTime(Long time) {
        return Integer.parseInt(String.valueOf(time).substring(0, 8));
    }

    public static void main(String[] args) {
        System.out.println(getCurrentDate());
        System.out.println(getCurrentTime());
    }
}
